<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/21 0021
 * Time: 22:40
 */

namespace Lib\Core\WebSocket;

/**
 * @description:保存当前客户端上下文信息
 * @auth:yfl
 * @email:554665488@qq.com
 * Class WebSocketContext
 * @package Lib\Core\WebSocket
 */
class WebSocketContext
{
    /**
     * fd=>[
     *    $fd
     *    $path
     *    $request
     *
     * ]
     */
    private static $connection = [];

    //初始化
    public static function init($fd, $path, $request)
    {
        self::$connection[$fd]['path'] = $path;
        self::$connection[$fd]['request'] = $request;
    }

    //获取连接信息
    public static function get($fd)
    {
        if (!isset($fd)) return false;
        return self::$connection[$fd]['path'] ?? null;
    }

    //获取请求客户端对象
    public static function getRequest($fd)
    {
        if (!isset($fd)) return false;
        return self::$connection[$fd]['request'] ?? null;
    }


    //删除方法
    public static function del($fd)
    {
        if (!isset($fd)) return false;
        if (isset(self::$connection[$fd])) unset(self::$connection[$fd]);
    }
}